package com.qingzhou.web.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import com.qingzhou.web.repository.dao.TreatmentDao;
import com.qingzhou.web.repository.entity.TreatmentEntity;
import com.qingzhou.web.repository.mapper.TreatmentMapper;
import com.qingzhou.web.service.DataCheckLogService;
import com.qingzhou.web.service.EdcConstants;
import com.qingzhou.web.service.TreatmentService;
import com.qingzhou.web.service.ConvertHelper;
import com.qingzhou.web.service.impl.params.request.*;
import com.qingzhou.web.service.impl.params.vo.TreatmentVo;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

@Component
public class TreatmentServiceImpl extends ServiceImpl<TreatmentMapper, TreatmentEntity> implements TreatmentService {
    @Inject
    TreatmentDao treatmentDao;
    @Inject
    ConvertHelper convertHelper;
    @Inject
    DataCheckLogService dataCheckLogService;

    @Override
    public int add(TreatmentAddForm form) {
        Assert.isTrue(StrUtil.isNotEmpty(form.getPatientCode()),"患者编号不允许为空");

        TreatmentEntity entity = BeanUtil.copyProperties(form, TreatmentEntity.class);
        entity.setStatus("create");
        return treatmentDao.add(entity);
    }

    @Override
    public int delete(TreatmentDeleteForm form) {
        TreatmentEntity entity = treatmentDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");
        Assert.isTrue(StrUtil.equalsIgnoreCase("create",entity.getStatus()),"该状态不允许删除");

        return treatmentDao.delete(form.getId());
    }

    @Override
    public Page<TreatmentVo> page(Page<TreatmentEntity> page, SubTablePageForm form) {
        Page<TreatmentEntity> paginate = treatmentDao.getMapper().paginate(page, QueryWrapper.create()
                .in("patient_code",form.getPatientCodeList())
                .orderBy("create_time desc"));
        return convertHelper.convertTreatment(paginate);
    }

    @Override
    public TreatmentVo detail(TreatmentDetailForm form) {
        TreatmentEntity entity = treatmentDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");
        return convertHelper.convert(entity);
    }

    @Override
    public int edit(TreatmentEditForm form) {
        TreatmentEntity entity = treatmentDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");
        Assert.isTrue(StrUtil.equalsIgnoreCase("create",entity.getStatus()),"该状态不允许编辑");

        BeanUtil.copyProperties(form, entity,"patientCode"); //患者编号不能变
        return treatmentDao.update(entity);
    }

    @Override
    public int forceEdit(TreatmentForceEditForm form) {
        TreatmentEntity entity = treatmentDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");
        Assert.isTrue(StrUtil.equalsIgnoreCase("done",entity.getStatus()),"该状态不允许数据检查");

        JSONObject diffContent = DataCheckLogServiceImpl.diff(entity, form, new String[]{});
        BeanUtil.copyProperties(form, entity,"patientCode"); //患者编号不能变
        int rows = treatmentDao.update(entity);

        if (rows > 0 && MapUtil.isNotEmpty(diffContent)) {
            DataCheckLogAddForm addForm = new DataCheckLogAddForm(EdcConstants.EDC_TREATMENT, diffContent, entity.getPatientCode());
            dataCheckLogService.add(addForm);
        }
        return rows;
    }

    @Override
    public int done(TreatmentDoneForm form) {
        TreatmentEntity entity = treatmentDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");
        Assert.isTrue(StrUtil.equalsIgnoreCase("create",entity.getStatus()),"该状态不允许核实");

        BeanUtil.copyProperties(form, entity,"patientCode"); //患者编号不能变
        entity.setStatus("done");
        return treatmentDao.update(entity);
    }

    @Override
    public int stop(TreatmentStopForm form) {
        TreatmentEntity entity = treatmentDao.getById(form.getId());
        Assert.isTrue(ObjUtil.isNotNull(entity),"数据不见了");
        Assert.isTrue(!StrUtil.equalsIgnoreCase("stop",entity.getStatus()),"该状态不允许终止");

        entity.setStatus("stop");
        entity.setStopReason(form.getStopReason());
        return treatmentDao.update(entity);
    }
}
